package com.example.tellwin.mine.presenter;

import com.example.tellwin.api.TwjfApi;
import com.example.tellwin.base.RxPresenter;
import com.example.tellwin.base.SimpleCallback;
import com.example.tellwin.mine.bean.AttentionQuestionContentBean;
import com.example.tellwin.mine.contract.AttentionQuestionContract;
import com.example.tellwin.utils.HttpUtils;

import javax.inject.Inject;

import okhttp3.RequestBody;

/**
 * @author : LinZePeng
 * @date : 2020/12/4
 * @Describe ：
 **/
public class AttentionQuestionPresenter extends RxPresenter<AttentionQuestionContract.View> implements AttentionQuestionContract.Presenter<AttentionQuestionContract.View> {

    private TwjfApi mApi;

    private final int pageSize = 10;
    private int questionPages;
    private int questionPageNum;
    private boolean isNotMoreQuestion;
    private int teacherPages;
    private int teacherPageNum;
    private boolean isNotMoreTeacher;

    @Inject
    public AttentionQuestionPresenter(TwjfApi mApi) {
        this.mApi = mApi;
    }


    @Override
    public void getAttentionQuestionList(boolean isLoadMore) {
        if(isLoadMore){
            if (isNotMoreQuestion) {
                return;
            }
            if (questionPages != 0 && questionPageNum == questionPages) {
                isNotMoreQuestion = true;
                return;
            }
            questionPageNum++;
        }else {
            questionPageNum = 1;
            questionPages = 0;
            isNotMoreQuestion = false;
        }

        mApi.getAttentionQuestionList("1" , questionPageNum+"",pageSize+"", new SimpleCallback<AttentionQuestionContentBean>(mView) {
            @Override
            public void onSuccess(AttentionQuestionContentBean attentionQuestionContentBean) {
                questionPageNum = attentionQuestionContentBean.getPageNum();
                questionPages = attentionQuestionContentBean.getPages();
                if (attentionQuestionContentBean.getMyFollowList() == null || attentionQuestionContentBean.getMyFollowList().size() == 0) {
                    isNotMoreQuestion = true;
                    return;
                }
                mView.attentionQuestionListResult(isLoadMore , attentionQuestionContentBean.getMyFollowList());

            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void likeQuestion(String questionId, int position) {
        RequestBody requestBody = HttpUtils.getRequestBody("followType", "0" + "", "relationId", questionId, "relationType", "0");
        mApi.follow(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.likeQuestionResult(position);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }

    @Override
    public void attention(int position , String questionId) {
        RequestBody requestBody = HttpUtils.getRequestBody("followType", "1" + "", "relationId", questionId, "relationType", "0");
        mApi.follow(requestBody, new SimpleCallback<Object>(mView) {
            @Override
            public void onSuccess(Object o) {
                mView.attention(position ,questionId);
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }
}
